import { reqGetReserveList } from '@/api/reserve';
import { action, observable } from 'mobx-miniprogram';

/**
 * 预定模块的数据仓库
 * @author: Ruan Jiazhen
 * @date: 2024-04-10 09:32:04
 **/
export const reserveStore = observable({
  /** 预定数据列表 */
  reserveList: [] as ResReserveInfoItem[],

  /** 预定数据列表分页参数 */
  reservePagination: {
    page: 1,
    pageSize: 10,
    total: -1,
  },

  /** 获取预定数据列表 */
  getReserveList: action(async function () {
    if (this.reservePagination.total !== -1 && this.reserveList.length >= this.reservePagination.total) {
      wx.showToast({ title: '没有更多了', icon: 'none' });
      return;
    }
    try {
      const res = await reqGetReserveList(this.reservePagination.page, this.reservePagination.pageSize);
      if (res.code !== 200) {
        throw new Error(res.msg);
      }

      this.reserveList = this.reserveList.concat(res.data.list);
      this.reservePagination = {
        ...this.reservePagination,
        total: res.data.totalResult,
        page: this.reservePagination.page + 1,
      };
    } catch (e: any) {
      wx.showToast({ title: e.message, icon: 'none' });
    }
  }),

  /** 重置预定数据 */
  resetReserveList: action(function () {
    this.reserveList = [];
    this.reservePagination = {
      page: 1,
      pageSize: 10,
      total: -1,
    };
  }),
});
